﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AmaroK86.Tools;

namespace AmaroK86.MassEffect3.Texplorer
{
    public partial class frmPreferences : Form
    {
        string defaultGamePath;
        INIHandler iniHnd = new INIHandler();

        public frmPreferences()
        {
            InitializeComponent();
        }

        private void updateTexts()
        {
            tbxMainPath.Text = ME3Paths.gamePath;
            tbxCookedPath.Text = ME3Paths.cookedPath;
            tbxTOCbin.Text = ME3Paths.tocFile;
            tbxSaveGamePath.Text = ME3Paths.BioWareDocPath;
            tbxGameINI.Text = ME3Paths.GamerSettingsIniFile;
        }

        private void frmPreferences_Load(object sender, EventArgs e)
        {
            defaultGamePath = ME3Paths.gamePath;

            string tfcUseDefault = iniHnd.IniReadValue("Texplorer", "tfcDefault");
            if (tfcUseDefault == null || tfcUseDefault == "" || !Convert.ToBoolean(tfcUseDefault))
                rdbNew.Checked = true;
            else
                rdbDef.Checked = true;

            string tfcFileName = iniHnd.IniReadValue("Texplorer", "tfcFileName");
            if (tfcFileName != null && tfcFileName != "")
                tbxTfcFileName.Text = tfcFileName;

            updateTexts();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ME3Paths.gamePath = defaultGamePath;
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (rdbNew.Checked)
            {
                iniHnd.IniWriteValue("Texplorer", "tfcDefault", false.ToString());
                iniHnd.IniWriteValue("Texplorer", "tfcFileName", tbxTfcFileName.Text);
            }
            else
                iniHnd.IniWriteValue("Texplorer", "tfcDefault", true.ToString());
            this.Close();
        }

        private void lblMainGame_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start(ME3Paths.gamePath);
        }

        private void lblCookedPath_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start(ME3Paths.cookedPath);
        }

        private void lblTOCbin_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start(ME3Paths.tocFile);
        }

        private void lblSaveGamePath_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start(ME3Paths.BioWareDocPath);
        }

        private void lnkGameINI_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start(ME3Paths.GamerSettingsIniFile);
        }

        private void btnChangePath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            folderDialog.ShowNewFolderButton = false;
            folderDialog.SelectedPath = ME3Paths.gamePath;
            DialogResult dialogResult = folderDialog.ShowDialog();
            while (dialogResult == DialogResult.OK && !File.Exists(folderDialog.SelectedPath + @"\Binaries\Win32\MassEffect3.exe"))
            {
                MessageBox.Show("Invalid folder, please select the correct location of Mass Effect 3", "Invalid folder", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                dialogResult = folderDialog.ShowDialog();
            }
            if(dialogResult == DialogResult.OK)
                ME3Paths.gamePath = folderDialog.SelectedPath + "\\";
            updateTexts();
        }
    }
}
